#include "Modulation.h"
#include "../Util/Util.h"
#include <stdlib.h>

#ifndef FADE_IN
#define FADE_IN 1
#endif

#ifndef FADE_OUT
#define FADE_OUT 0
#endif

Modulation* newModulation() {
	Modulation* map;
	map = (Modulation*) malloc(sizeof(Modulation));
	map->init();
	return map;
}

void Modulation::init() {
	modulationDirection = FADE_IN;
	modulationCounter = 0;

	// public defaults
	modulationTime = 2000;
	minimum = 0.0;
	maximum = 1.0;
}

float Modulation::nextModulationValue() {
	float modulationAmount;
	if (modulationCounter >= modulationTime) {
		modulationCounter = 0;
		if (modulationDirection == FADE_IN) {
			modulationDirection = FADE_OUT;
		} else {
			modulationDirection = FADE_IN;
		}
	}
	
	modulationAmount = (1.0 / modulationTime) * modulationCounter;
	
	if (modulationDirection) {
		modulationAmount = modulationAmount;
	} else {
		modulationAmount = 1 - modulationAmount;
	}
	modulationCounter++;
	
	return mapFloat(modulationAmount, 0.0, 1.0, minimum, maximum);
}
